package edu.uestc.canary.admin.web.advise;

import edu.uestc.canary.entity.dto.Result;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局控制器增强通知(AOP)
 */
@ControllerAdvice("edu.uestc.canary.admin.web.controller")
@Slf4j
public class GlobalControllerAdvise {
    /**
     * 处理控制器下所有的异常
     */
    @ExceptionHandler
    @ResponseBody
    public Result error(Exception e){
        log.debug("error: / by zero");
        log.error(e.getMessage(), e);
        return Result.fail();
    }

    /**
     * 处理器参数校验失败异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Result error2(ConstraintViolationException e){
        log.error(e.getMessage(), e);
        return Result.fail(null, "参数不合法:" + e.getMessage());
    }
}
